﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RMS.Models;
using RMS.Models.Repository;
using System.Web.ModelBinding;
using System.Data.Entity;
using System.Net.Mail;
using System.Net;

namespace RMS.Pages
{
    public partial class Volunteer : System.Web.UI.Page
    {
        private Repository repo = new Repository();
        
        protected void Page_Load(object sender, EventArgs e) {
            Response.AddHeader("Expires", "0");
            
            if (!IsPostBack)
            {

                // Sections of the web form that are visible on page load.
                if (Session["Email"] == null)
                {
                    isLoggedInSection.Visible = true;
                    volunteerFormBody.Visible = false;
                    volunteerSummary.Visible = false;

                }
                else
                {
                    isLoggedInSection.Visible = false;

                    if (hasVolunteerRecord() == true)
                    {

                        volunteerFormBody.Visible = false;
                        volunteerSummary.Visible = true;
                    }
                    else
                    {
                        volunteerFormBody.Visible = true;
                        volunteerSummary.Visible = false;
                    }


                }

                volunteerSubmitMessage.Visible = false;

            }

            if (IsPostBack)
            {
                if (hasVolunteerRecord() == true)
                {
                    isLoggedInSection.Visible = false;
                    volunteerFormBody.Visible = false;
                    volunteerSummary.Visible = true;
                }
                else
                {
                    isLoggedInSection.Visible = false;
                    volunteerFormBody.Visible = true;
                    volunteerSummary.Visible = false;
                }
            }
        }

        protected void btnSubmitVolunteerForm_Click(object sender, EventArgs e)
        {
            if (hasVolunteerRecord() == true)
            {
                isLoggedInSection.Visible = false;
                volunteerFormBody.Visible = false;
                volunteerSummary.Visible = true;
                volunteerSubmitMessage.Visible = false;
                return;
            }
            
            VolunteerItem VItemtoAdd;
          
            /* Insert VolunteerItem database records based on selected check boxes. */
            if (cbBulkMailing.Checked == true) {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Bulk Mailing";
                VItemtoAdd.VolunteerItemCategory = "Office Assistance";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbCarpentry.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Carpentry";
                VItemtoAdd.VolunteerItemCategory = "Maintenance";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbBrushRemoval.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Brush Removal";
                VItemtoAdd.VolunteerItemCategory = "Landscaping";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbComputerMaintenance.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Computer Maintenance";
                VItemtoAdd.VolunteerItemCategory = "Office Assistance";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbElectrical.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Electrical";
                VItemtoAdd.VolunteerItemCategory = "Maintenance";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbFlowerAndPlantCare.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Flower and Plant Care";
                VItemtoAdd.VolunteerItemCategory = "Landscaping";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbDataEntry.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Data Entry";
                VItemtoAdd.VolunteerItemCategory = "Office Assistance";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbHVAC.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "HVAC";
                VItemtoAdd.VolunteerItemCategory = "Maintenance";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbLeafRaking.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Leaf Raking";
                VItemtoAdd.VolunteerItemCategory = "Landscaping";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbGeneralOfficeAid.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "General Office Aid";
                VItemtoAdd.VolunteerItemCategory = "Office Assistance";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbPainting.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Painting";
                VItemtoAdd.VolunteerItemCategory = "Maintenance";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbTreeAndBushPruning.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Tree and Bush Pruning";
                VItemtoAdd.VolunteerItemCategory = "Landscaping";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbLibraryAid.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Library Aid";
                VItemtoAdd.VolunteerItemCategory = "Office Assistance";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbPlumbing.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Plumbing";
                VItemtoAdd.VolunteerItemCategory = "Maintenance";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbWeeding.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Weeding";
                VItemtoAdd.VolunteerItemCategory = "Landscaping";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbWordProcessing.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Word Processing";
                VItemtoAdd.VolunteerItemCategory = "Office Assistance";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbRefrigeration.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Refrigeration";
                VItemtoAdd.VolunteerItemCategory = "Maintenance";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbRoofingOrGutters.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Roofing or Gutters";
                VItemtoAdd.VolunteerItemCategory = "Maintenance";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbRoadRepair.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Road Repair";
                VItemtoAdd.VolunteerItemCategory = "Maintenance";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbSmallEngineRepair.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Small Engine Repair";
                VItemtoAdd.VolunteerItemCategory = "Maintenance";
                InsertVolunteerItem(VItemtoAdd);
            }

            if (cbWindowWashing.Checked == true)
            {
                VItemtoAdd = new VolunteerItem();
                VItemtoAdd.User_Us_ID = (int)Session["ID"];
                VItemtoAdd.VolunteerItemType = "Window Washing";
                VItemtoAdd.VolunteerItemCategory = "Maintenance";
                InsertVolunteerItem(VItemtoAdd);
            }
                        
            
            // set visible sections of web form
            volunteerFormBody.Visible = false;
            volunteerSubmitMessage.Visible = true;
            volunteerSummary.Visible = true;
        }
        
        /*
        private IEnumerable<Retreat> FilterRetreats()
        {
            
        } */

        public IEnumerable<VolunteerItem> GetVolunteerItems()
        {
            return repo.VolunteerItems
            .OrderBy(p => p.VolunteerItemType);
        }

        public IEnumerable<VolunteerItem> GetCurrentVolunteerItems()
        {
            return repo.VolunteerItems
            .OrderBy(p => p.VolunteerItemType)
            .Where(p => p.User_Us_ID == (int)Session["ID"]);
        }

        protected Boolean hasVolunteerRecord() {
            
            foreach (RMS.Models.VolunteerItem vItem in GetVolunteerItems()) {
                if (Session["ID"] == null)
                {
                    return false;
                }

                if (vItem.User_Us_ID == (int)Session["ID"])
                {
                    return true;
                }
            }

            return false;
        }


        protected void btnLogin_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Login.aspx");
        }

        protected void btnCreateAccount_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/CreateAccount.aspx");
        }

        // Method for inserting a VolunteerItem.
        public void InsertVolunteerItem(VolunteerItem myVolunteerItem)
        {
            //VolunteerItem myVolunteerItem = new VolunteerItem();

            if (TryUpdateModel(myVolunteerItem, new FormValueProvider(ModelBindingExecutionContext)))
            {
                repo.SaveVolunteerItem(myVolunteerItem);
            }
        }

        protected void btnClearVolunteerRecord_Click(object sender, EventArgs e)
        {
            deleteVolunteerRecords();
            Response.Redirect("~/Pages/Volunteer.aspx");
        }

        protected void deleteVolunteerRecords()
        {
            IEnumerable<VolunteerItem> vItemsToDelete = GetCurrentVolunteerItems();
            
            foreach (VolunteerItem vItem in vItemsToDelete)
            {
                repo.DeleteVolunteerItem(vItem);
            }
        }

        protected string GenerateEmail()
        {
            string response = " ";

            try
            {
                MailMessage mailMessage = new MailMessage();
                
                // Custom Strings
                string email = (string)Session["Email"];
                string body = "Greetings,\n\n" + "Thank you for your consideration in volunteering at the Redemptorist Retreat Center!";

                mailMessage.To.Add(email);
                mailMessage.To.Add("rrc.maintenance@gmail.com");

                mailMessage.From = new MailAddress("rrc.maintenance@gmail.com");
                mailMessage.Subject = "Thank You for Volunteering at the Redemptorist Retreat Center!";
                mailMessage.Body = body;
                SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Host = "smtp.gmail.com";
                smtpClient.Port = 587;
                smtpClient.Credentials = new NetworkCredential("rrc.maintenance", "rrctest12345");
                smtpClient.EnableSsl = true;
                smtpClient.Send(mailMessage);

                response = "Confirmation e-mail sent to: " + email;
                Response.Write(response);
                
            }
            catch (Exception ex)
            {
                
                response = "Error: Could not sent e-mail... " + ex.Message;
                Response.Write(response);
            }

            return response;
        }
       
    }
}